package main

import (
	"errors"
	"fmt"
	perrors "github.com/pkg/errors"
)

type DBError struct {
	msg string
}

func (e *DBError) Error() string {
	return e.msg
}

var EmptyErr = perrors.New("name cannot empty")

var EmptyErr1 = &DBError{"name cannot empty"}

type Student struct {
	Name string
	Age  int
}

func (s *Student) SetName(name string) error {
	if s.Name == "" {
		return EmptyErr1
	}
	s.Name = name
	return nil
}

func NewStudent() (*Student, error) {
	student := &Student{
		Age: 18,
	}
	err := student.SetName("")
	if err != nil {
		return student, perrors.Wrap(err, "set name failed")
	}
	return student, nil
}

func main() {

	_, err := NewStudent()
	if errors.Is(err, EmptyErr) {
		fmt.Println("is empty")
	}

	var dberr *DBError
	if errors.As(err, &dberr) {
		fmt.Println("match")
	}
	//fmt.Printf("%+v\n", err)

}
